﻿@{
    ViewBag.Title = "DQ配置新增";
    Layout = "~/Views/Shared/_Main.cshtml";
}
@model  Model.DQConfig
@section Head{
    <script type="text/javascript">
        var UrlArray = $.getUrlArray();
        var modelID = UrlArray["modelID"];
        var nmID = UrlArray["nmID"];
        var dbName = encodeURI(UrlArray["dbName"]);
        var ndbName = dbName;
        if (nmID && nmID != "null") {
            ndbName = encodeURI(UrlArray["ndbName"]);
        }

        var retData = new Array();
        var update = false;
        var opts = {
            height: 420,
            url: "/DQConfig/GetFactTableColumns",
            idField: "GUID",
            para: { dqID: UrlArray["id"], modelID: modelID, dbName: ndbName },
            columns: [
                { field: 'DisplayName', title: '脚本', width: 25 },
                { field: 'ColumnDesc', title: '条件描述', width: 15, align: 'center' },
                { field: 'LegalType', title: 'DQ类型', width: 10, align: 'center' },
                { field: 'Operator', title: '操作符', width: 10, align: 'center' },
                { field: 'Rules', title: '规则', width: 20, align: 'center' },
                { field: 'UniqueConstraint', title: '唯一性约束', width: 10, align: 'center', formatter: function (value) {
                    if (value == 1)
                        return "是";
                    else
                        return "否";
                }
                },
                { field: 'GUID', title: '操作', width: 10, align: 'center', formatter: function (value, rowData, rowIndex) {
                    var add = true;
                    for (var j = 0; j < retData.length; j++) {
                        if (retData[j].GUID == value) {
                            add = false;
                        }
                    }
                    if (add)
                        retData.push(rowData);
                    var a = "<select onchange=\"DoAction(this.options.selectedIndex,'" + rowIndex + "')\"><option value=''>-请选择-</option>";
                    return a + "<option >修改</option><option >删除</option></select>";
                }
                }
                ]
        };
        function InitList() {
            $("#DataGrid").dg(opts);
        }
        function DoAction(i, v) {
            switch (i) {
                case 1:
                    AddDQ(v);
                    break;
                case 2:
                    Delete(v);
                    break;
            }
            InitList();
        }
        var insert = false;
        $(function () {
            $(".link a").each(function () {
                $(this).attr("href", $(this).attr("href") + "?modelID=" + modelID + "&dbName=" + dbName
                + "&nmID=" + UrlArray["nmID"] + "&ndbName=" + encodeURI(UrlArray["ndbName"]));
            })
            InitList();
            if (retData.length == 0)
                insert = true;
        });
        function AddDQ(v) {
            var urlPara = "?modelID=" + UrlArray["modelID"] + "&dbName=" + encodeURI(UrlArray["dbName"])
         + "&nmID=" + UrlArray["nmID"] + "&ndbName=" + encodeURI(UrlArray["ndbName"]) + "&dqid=" + v;
            showModalMany("Info", "/ModelConfig/EditViewScript" + urlPara, "860", "560", returnData);
        }
        function Delete(v) {
            if (confirm("确定要删除吗？")) {
                retData.removeAt(v);
                var data = { "total": 0, "rows": retData };
                opts["data"] = data;
                $("#DataGrid").dg(opts);
                update = true;
            }
        }
        function backIndex() {
            window.location.href = "/DQConfig/Index?modelID=" + modelID + "&dbName=" + dbName + "&nmID=" + nmID + "&ndbName=" + encodeURI(UrlArray["ndbName"]);
        }
        function returnData(v) {
            if (v.GUID == "") {
                v.GUID = retData.length;
                retData.push(v);
            }
            else {
                for (var j = 0; j < retData.length; j++) {
                    if (retData[j].GUID == v.GUID) {
                        retData[j] = v;
                    }
                }
            }
            var data = { "total": v.length, "rows": retData };
            opts["data"] = data;
            $("#DataGrid").dg(opts);
        }
        function save() {
            if ($.trim($("#txtDQ_Name").val()) == "") {
                alert("数据质量名称不能为空！");
                return;
            }
            saveDate();
        }
        function returnDate(v) {
            if (v == "1") {
                saveDate();
            }
        }
        function saveDate() {
            var tmpmodelID = modelID;
            var tmpdbName = dbName;
            if (nmID && nmID != "null") {
                tmpmodelID = nmID;
                tmpdbName = UrlArray["ndbName"];
            }
            $.post('/DQConfig/SaveData', { id: UrlArray["id"], modelID: tmpmodelID, dqName: $("#txtDQ_Name").val(), dqDesc: $("#txtDQ_Description").val(), gridData: $.toJSON(retData), dbName: tmpdbName }, function (result) {
                if (result == 1)
                    alert("操作成功！");
                else if (result == 2)
                    alert("存在相同的数据质量名称，请使用其他名称！");
                else
                    alert("操作失败！");
            });
        }
    </script>
}
<div style="margin: 5px;">
    <table class="listTitle link">
        <tr>
            <td width="15">
                <img src="/images/filesave.png" />
            </td>
            <td width="500">
                <a href="/ModelConfig/Index">模型管理</a> --> <a href="/ModelConfig/Edit">新增模型配置</a>
                --> <a href="/ODSConfig/ODSConfigAdd">选择源系统表</a> --> <a href="/DQConfig/Index">数据质量配置列表</a>
                --> 编辑数据质量配置
            </td>
            <td align="right">
                <input type="button" class="button" value="完成" onclick="save()" />
                <input type="button" class="button" value="返回" onclick="backIndex()" />
            </td>
        </tr>
    </table>
    <table class="searchArea" width="100%">
        <tr>
            <td width="100" align="right">
                <span class="red">*</span>数据质量名称：
            </td>
            <td>
                <input type="text" id="txtDQ_Name" value="@Model.DQ_Name" style="width: 350px;" />
            </td>
        </tr>
        <tr>
            <td valign="top" align="right">
                数据质量描述：
            </td>
            <td>
                <textarea rows="3" id="txtDQ_Description" style="width: 350px;">@Model.DQ_Description</textarea>
            </td>
        </tr>
    </table>
    <table class="listTitle">
        <tr>
            <td width="15">
                <img src="/images/edit.png" />
            </td>
            <td width="300">
                数据质量字段配置
            </td>
            <td align="right">
                <input type="button" class="button" value="新增" onclick="AddDQ('');" />
            </td>
        </tr>
    </table>
    <table id="DataGrid" class="pagTable" cellpadding="0" cellspacing="0">
    </table>
</div>
